//
//  XuKeViewController.m
//  Yu01
//
//  Created by 蒋松涵 on 16/8/20.
//  Copyright © 2016年 蒋松涵. All rights reserved.
//

#import "XuKeViewController.h"
#import "MessageCell.h"
#import "TermNumberCourseModel.h"


@interface XuKeViewController ()<UITableViewDelegate,UITableViewDataSource>
{
    UITableView * _tableView;
}

@property(nonatomic,strong)NSMutableArray * dataArray;
@property (nonatomic,retain) NSString *urlStr;
@property (nonatomic,retain) NSDictionary *urlParameters;

@end

@implementation XuKeViewController


#pragma mark - 数据请求
//请求数据
- (void)fecthDataFromNet:(NSString *)url parameters:(NSDictionary *)parameter {
    
    NSLog(@"%@",url);
    NSLog(@"%@",parameter);
    //初始化请求工具
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    [manager POST:url parameters:parameter progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        NSMutableDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:nil];
        
        NSLog(@"~~~~%@",dic[@"rows"]);
        NSMutableArray * List = dic[@"rows"];
        for (NSDictionary * dicList in List)
        {
            TermNumberCourseModel * model = [[TermNumberCourseModel alloc]init];
            [model setValuesForKeysWithDictionary:dicList];
            [self.dataArray addObject:model];
            NSLog(@"----%ld",self.dataArray.count);
        }
        [_tableView reloadData];
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        
        
    }];
}







- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"学期课表";
    
//    CGFloat h = 736 / ScreenHeight;
//    CGFloat w = 414 / ScreenWidth;
    
    
    _urlStr = @"http://www.100xsys.com.cn/school/app/coursePlanInfo/yearTermListJson";
    _urlParameters = [[NSDictionary alloc]initWithObjectsAndKeys:@"2016",@"coursePlanInfo .year",@"1",@"coursePlanInfo .termNumber",@"1",@"studentInfoId",@"1",@"appWebConfigId", nil];
    
    [self fecthDataFromNet:_urlStr parameters:_urlParameters];
    
    self.dataArray = [[NSMutableArray alloc]init];
    
    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.rowHeight = 200;
    [self.view addSubview:_tableView];

    [_tableView registerNib:[UINib nibWithNibName:@"MessageCell" bundle:nil] forCellReuseIdentifier:@"cell"];
}






#pragma mark - tableView的代理方法
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MessageCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell)
    {
        cell = [[MessageCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    TermNumberCourseModel * model = self.dataArray[indexPath.row];
    
    cell.namelabel.text = model.name;
    cell.xuefenlabel.text = model.credits;
    cell.fanweilabel.text = model.zoneName;
    cell.schoollabel.text = [NSString stringWithFormat:@"%@ %@-%@",model.buildingName,model.floor,model.classroomNumber];
    
    return cell;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
